package biz.datalk.industrialland.admin.config.properties;

import biz.datalk.commons.utils.json.JsonUtil;
import com.google.common.collect.Lists;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.Assert;

import java.io.Serializable;
import java.util.List;

/**
 * @author tarofang@163.com
 * @date 2024-01-09
 */
@Slf4j
@Getter
@Setter
@ToString
@ConfigurationProperties(IndustrialLandCoreProperties.PREFIX)
public class IndustrialLandCoreProperties implements InitializingBean {
    public static final String PREFIX = "industrial-land";

    /** 使用图形验证码 */
    private Boolean loginUseImgCaptcha = true;

    /** 同一个IP 每分钟请求 10 次 */
    private Integer apiAuthCodeSameIpPreMinuteLimit = 10;

    /** 短信验证码的长度 */
    private Integer apiAuthCodeCodeLength = 6;

    /** 验证码的有效时长 */
    private Long apiAuthCodeCodeTimeoutSeconds = 120L;

    /** 万能验证码, 不配置时表示不启用 */
    private String apiAuthLoginSupportCode;

    /**
     * 每周的 休息日, 默认 周六 周日
     * 1-7 表示 周一 到 周日
     */
    private List<Integer> offDayOfWeeks = Lists.newArrayList(6, 7);

    /**
     * 审核超时时间
     * 从提交日期开始计算
     * 默认 0 个工作日(不启用)
     */
    private Integer auditTimeoutWorkDay = 0;
    /**
     * 支付超时时间
     * 从审核通过的时间开始计算
     * 默认 3 个工作日
     */
    private Integer payTimeoutWorkDay = 3;

    /**
     * 协议签署超时时间
     * 从审核车场通过的时间开始计算
     * 默认 3 个工作日
     */
    private Integer argumentTimeoutWorkDay = 3;

    /**
     * /api/auth/change_token 接口 校验配置
     */
    private ApiAuthChangeToken apiAuthChangeToken;

    /**
     * 是否启用强制开通功能， 默认不启用
     */
    private Boolean apiParkingApplyOpenUseForceOpen = false;

    @Override
    public void afterPropertiesSet() throws Exception {
        log.debug("[IndustrialLandCoreProperties={}]", JsonUtil.getJsonFromObject(this, Boolean.TRUE));
        Assert.isTrue(CollectionUtils.isNotEmpty(offDayOfWeeks), "The 'industrial-land.off-day-of-weeks' must not be blank.");
        for (Integer offDayOfWeek : offDayOfWeeks) {
            Assert.isTrue(offDayOfWeek >= 1 && offDayOfWeek <= 7, "The 'industrial-land.off-day-of-weeks' must be in range of 1 to 7.");
        }
        Assert.isTrue(auditTimeoutWorkDay != null && auditTimeoutWorkDay >= 0, "The 'industrial-land.audit-timeout-work-day' must be greater than or equal to 0.");
        Assert.isTrue(payTimeoutWorkDay != null && payTimeoutWorkDay >= 0, "The 'industrial-land.pay-timeout-work-day' must be greater than or equal to 0.");
        Assert.notNull(apiAuthChangeToken, "The 'industrial-land.api-auth-change-token' must not be null.");
    }

    @Getter
    @Setter
    @ToString
    public static class ApiAuthChangeToken implements Serializable {
        private static final long serialVersionUID = 1L;

        /** token 中 username 的标识 Key */
        private String usernameKey = "username";
        /** 校验 token 是否存在 */
        private Boolean checkOtherTokenExists = true;
        /** token 的 redis key 的前缀 */
        private String tokenRedisKeyPrefix = "prefix-user-token-";
        private Integer tokenRedisDb = 0;
        private String tokenRedisHost = "localhost";
        private Integer tokenRedisPort = 6379;
        private String tokenRedisPassword = "";

    }
}
